import { _decorator } from 'cc';
import { PanelModel } from '../../Basic/UI/PanelModel';
import { GameParams, PlayerParams } from '../../Basic/Config/DataConfig';
import { WebRequestHttp, RequestUrl } from '../../Basic/Handle/WebRequest';
import { DataMgr, GameData, PlayerData } from '../../Basic/Manager/DataManager';
import { MultiPlatform, Platform } from '../../Basic/Model/Platform/MultiPlatform';
const { ccclass } = _decorator;

@ccclass('RankModel')
export class RankModel extends PanelModel {
  className: string = 'RankModel';
  loadRes: string = 'maingame/dialog/RankPanel';
  basePanel;
  reqP(data) {
    console.log('reqP', data);
    switch (MultiPlatform.Ins().platform) {
      case Platform.tt:
      case Platform.wx:
        if (!data?.data?.type) break;
        DataMgr.changePlayerAttr(PlayerParams.myRankData, data.data.rank > 0 ? [data.data] : [], true);
        break;
      default:
        if (!data?.type) break;
        DataMgr.changePlayerAttr(PlayerParams.myRankData, data.rank > 0 ? [data] : [], true);
        break;
    }

    // eslint-disable-next-line @typescript-eslint/no-unused-expressions
    this.basePanel && this.basePanel.updateMyRank();
  }

  getRank() {
    const data = {
      platform: MultiPlatform.Ins().platform,
      type: 'cake',
      session_key: PlayerData.get(PlayerParams.session_key),
    };
    WebRequestHttp.requestGet(RequestUrl.api_url + RequestUrl.LEADERBOARD_PLAYER, data, this.reqP, this);
  }

  /** 更新排行数据 */
  updateRank() {
    const score = GameData.get(GameParams.gameLevel);
    const myRankData = PlayerData.get(PlayerParams.myRankData);
    if ((myRankData.score == null || myRankData.score < score) && score > 0) {
      const data = {
        platform: MultiPlatform.Ins().platform,
        type: `cake`,
        score: score,
        session_key: PlayerData.get(PlayerParams.session_key),
        nickname: PlayerData.get(PlayerParams.nickname),
        avatarURL: PlayerData.get(PlayerParams.avatarUrl),
        mvpSymbolID: 0,
        estraData: ``,
      };
      // WebRequestHttp.requestPost(RequestUrl.api_url + RequestUrl.LEADERBOARD_ADD, data, this.reqP, this);
    }
  }
}
